<?php

namespace Biz\service\TodayNiceGoods;


use Biz\model\TodayNiceGoods\TodayNiceGoodsModel;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Doraemon\tools\ImgUrl;


/**
 * Description: 新品速递 service
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/5/20
 * Time: 11:45
 */
class TodayNIceGoodsService
{
    public const ACT_NUM = 7;//活动数量

    /**
     * 获取活动中wiki列表
     * @param $field
     * @param $limit
     * @param bool $is_show_all
     * @return array
     */
    public static function getRecentActLists($field, $limit, bool $is_show_all): array
    {
        $todayTime = date("Y-m-d 00:00:00");
        if ($is_show_all) {
            $where['status[!]'] = TodayNiceGoodsModel::STATUS_DELETE;
//            $where['end_at[>]'] = $now;
            $where["ORDER"] = [
                "start_at" => "DESC"
            ];
        } else {
//            $where['start_at[<=]'] = $now;
            $where['start_at[<=]'] = $todayTime;
            $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
            $where["ORDER"] = [
                "sort" => "DESC",
                "start_at" => "DESC"
            ];
        }
        $where["LIMIT"] = $limit;
        $list = TodayNiceGoodsModel::all($where, $field);
        if (!$list) {
            return [];
        }
        return $list;
    }


    /**
     * 获取今日活动中wiki列表
     * @param $where
     * @param $field
     * @param $start
     * @param $offset
     * @return array
     */
    public static function getActLists($where, $field, $start, $offset): array
    {
        $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
        $where["LIMIT"] = [$start, $offset];
        $now_time = date("Y-m-d H:i:s");
        $where['start_at[<=]'] = $now_time;
        $where['end_at[>]'] = $now_time;
        $list = TodayNiceGoodsModel::all($where, $field);
        if (!$list) {
            return [];
        }
        return $list;
    }


    /**
     * 获取往期活动列表
     * @param $field
     * @param $start
     * @param $offset
     * @param $month
     * @return array
     */
    public static function getBeforeList($field, $start, $offset, $month)
    {
        $where['end_at[<=]'] = date("Y-m-d 23:59:59");
        $where['month'] = $month;
        $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
        $where["LIMIT"] = [$start, $offset];
        $where['ORDER'] = [
            'end_at' => 'DESC'
        ];
        $list = TodayNiceGoodsModel::all($where, $field);
        if (!$list) {
            return [];
        }
        return $list;
    }

    /**
     * 获取某期活动数量
     * @param $month
     * @return bool|int|mixed|string
     */
    public static function getBeforeListCount($month)
    {
        $where['end_at[<=]'] = date("Y-m-d 00:00:00");
        $where['month'] = $month;
        $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
        return TodayNiceGoodsModel::count($where);

    }

    /**
     * 获取单条活动单个字段
     * @param $id
     * @param string $field
     * @return array|bool
     */
    public static function getActInfo($id, $field = 'id')
    {
        $where['id'] = $id;
        return TodayNiceGoodsModel::value($where, $field);
    }


    /**
     * 通过品牌id获取品牌活动数量
     * @param $brand_id
     * @return array|bool
     */
    public static function getActNumByBrandId($brand_id)
    {
        $where['status[!]'] = TodayNiceGoodsModel::STATUS_NORMAL;
        $where['brand_id'] = $brand_id;
        return TodayNiceGoodsModel::count($where);
    }

    /**
     * 审核操作
     * @param $params
     * @return bool
     */
    public static function audit($params): bool
    {
        $where['id'] = $params['id'];
        $data['status'] = $params['status'];
        $data['updated_at'] = date('Y-m-d H:i:s');
        $data['admin_id'] = $params['admin_id'];
        TodayNiceGoodsModel::update($data, $where);
        return true;
    }


    /**
     * 增加活动虚拟状态
     * @param $list
     * @return array
     */
    public static function formatList($list): array
    {
        $now = time();
        foreach ($list as &$info) {
            if (isset($info['start_at'], $info['end_at'], $info['status'])) {
                $start_at = strtotime($info['start_at']);
                $end_at = strtotime($info['end_at']);
                if ((int)$info['status'] === TodayNiceGoodsModel::STATUS_NORMAL) {
                    if ($start_at <= $now && $end_at > $now) {
                        $info['status'] = TodayNiceGoodsModel::STATUS_ACTING; //活动中
                    } else if ($end_at <= $now) {
                        $info['status'] = TodayNiceGoodsModel::STATUS_END; //活动结束
                    } else if ($start_at >= $now) {
                        $info['status'] = TodayNiceGoodsModel::STATUS_WAIT; //未开始
                    }
                }
                $act_status_arr = TodayNiceGoodsModel::STATUS_ARR;
                $info['status_name'] = $act_status_arr[$info['status']];
            }

            if (isset($info['img']) && $info['img']) {
                $info['img_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info['img'], ImgUrl::IMAGE_SIZE_ORI_PHONE_BIG) ?: '';
            }
            if (isset($info['back_img']) && $info['back_img']) {
                $info['back_img_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info['back_img'], ImgUrl::IMAGE_SIZE_ORI_PHONE_BIG) ?: '';
            }
            if (isset($info['back_show_img']) && $info['back_img']) {
                $info['back_show_img_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $info['back_show_img'], ImgUrl::IMAGE_SIZE_ORI_PHONE_BIG) ?: '';
            }
            if (isset($info['tags'])) {
                $info['tags_list'] = !empty($info['tags']) ? explode(",", $info['tags']) : [];
            }
            if (isset($info['end_at']) && $info['end_at']) {
                $info['date'] = date("m/d", strtotime($info['end_at']));
                $info['date_time'] = strtotime($info['end_at']);
            }
        }
        return $list;
    }


    /**
     * 通过id集合获取活动信息
     * @param $ids
     * @param string $field
     * @return array
     */
    public static function getActByIds($ids, $field = '*')
    {
        $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
        $where['id'] = $ids;
        return TodayNiceGoodsModel::all($where, $field);
    }


    /**
     * 获取月份列表
     * @return array
     */
    public static function getMonthLists(): array
    {
        $where['status'] = TodayNiceGoodsModel::STATUS_NORMAL;
        $where['month[<=]'] = date("Y-m");
        $where["ORDER"] = [
            "month" => "ASC"
        ];
        $where["GROUP"] = "month";
        $list = TodayNiceGoodsModel::all($where, ["month"]);
        if (!$list) {
            return [];
        }
        return $list;
    }

    public static function getTodayNiceGood()
    {
        $data = [];
        $where = [
            'status' => TodayNiceGoodsModel::STATUS_NORMAL,
            'start_at' => date("Y-m-d 00:00:00"),
        ];
        $info = TodayNiceGoodsModel::first($where);
        if ($info) {
            $wikiId = $info['wiki_id'];
            $list = clsWiki::getWikiBaseInfos([$wikiId]);
            $wikiInfo = $list[$wikiId] ?: [];
            $data = $wikiInfo ? [
                'id' => $info['id'],
                'wiki_id' => $wikiId,
                'title' => $info['title'],
                'sec_title' => $info['sec_title'],
                'cover_img' => $wikiInfo['cover_img']
            ] : [];
        }
        return $data;
    }

}
